import 'package:cloud_medicine/components/tag.dart';
import 'package:cloud_medicine/components/util.dart';
import 'package:cloud_medicine/http/post_api.dart';
import 'package:cloud_medicine/model/search.dart';
import 'package:cloud_medicine/pages/post_detail.dart';
import 'package:cloud_medicine/pages/prescription_detail.dart';
import 'package:cloud_medicine/pages/shop_detail.dart';
import 'package:cloud_medicine/static/search.dart';
import 'package:flutter/material.dart';

class SearchPage extends StatefulWidget {
  @override
  _SearchPageState createState() => _SearchPageState();
}

class _SearchPageState extends State<SearchPage> {
  final TextEditingController controller = TextEditingController();
  bool _loading = false;
  List<SearchItem> _list = [];

  Widget _buildTextField() {
    //theme设置局部主题
    return TextField(
      onChanged: (value) {
        _loadData(value);
      },
      controller: controller,
      cursorColor: Colors.black,
      //设置光标
      decoration: InputDecoration(
          border: InputBorder.none,
          icon: Icon(Icons.search),
          hintText: "搜索",
          hintStyle: new TextStyle(fontSize: 16, color: Colors.grey)),
      style: new TextStyle(fontSize: 16, color: Colors.black),
    );
  }

  // 加载数据
  Future<Null> _loadData(String value) async {
    _list.clear();
    setState(() {
      _loading = true;
    });
    dynamic res = await search_api({
      'content': value,
    });
    res['data'].forEach((dynamic searchItem) {
      SearchItem searchItemModel =
      SearchItem.init(searchItem);
      if (mounted) {
        setState(() {
          _list.add(searchItemModel);
        });
      }
    });
    if (mounted) {
      setState(() {
        _loading = false;
      });
    }
  }

  _entryVariousPage(String type, int id) {
    Map<String, Widget> _constractPages = {
      'post': PostDetail(postID: id),
      'shop': ShopDetail(shopid: id),
      'prescription': PrescriptionDetailPage(id: id),
    };
    Navigator.of(context).push(
        new MaterialPageRoute(builder: (context) => _constractPages[type]));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: false,
        title: Container(
          decoration: new BoxDecoration(
            border: Border.all(color: Colors.grey[200], width: 1.0), //灰色的一层边框
            color: Colors.grey[200],
            borderRadius: new BorderRadius.all(new Radius.circular(15.0)),
          ),
          alignment: Alignment.center,
          height: 36,
          padding: EdgeInsets.fromLTRB(10.0, 0.0, 0.0, 0.0),
          child: _buildTextField(),
        ),
        actions: <Widget>[
          new Center(
            child: Padding(
              padding: const EdgeInsets.all(8.0),
              child: GestureDetector(
                child: Text("取消"),
                onTap: () {
                  Navigator.pop(context);
                },
              ),
            ),
          )
        ],
      ),
      body: _loading
          ? loadingWidget(_loading)
          : ListView.builder(
              itemCount: _list.length,
              itemBuilder: (BuildContext context, int index) {
                return InkWell(
                  onTap: () {
                    _entryVariousPage(_list[index].type, _list[index].id);
                  },
                  child: Container(
                      decoration: BoxDecoration(
                        border: Border(
                          bottom: BorderSide(
                            width: 1,
                            color: Colors.grey,
                          ),
                        ),
                      ),
                      padding: EdgeInsets.all(10),
                      child: Row(
                        children: <Widget>[
                          Icon(
                            searchIcons[_list[index].type],
                            size: 40,
                          ),
                          SizedBox(width: 10),
                          Expanded(
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: <Widget>[
                                Tag(
                                  text: searchTagTexts[_list[index].type],
                                  bgColor: Colors.grey,
                                  fontSize: 12,
                                ),
                                SizedBox(height: 5),
                                Text(
                                  _list[index].abstract,
                                  maxLines: 1,
                                  overflow: TextOverflow.ellipsis,
                                ),
                              ],
                            ),
                          )
                        ],
                      )),
                );
              },
            ),
    );
  }
}
